<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            left: 100px;
            top: 100px;
        }
    </style>
</head>

<body>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
</body>
<script>

    // 问题1:  posX  posY 变量跨作用域 => 提取到两个作用域都能访问的位置(dragEle)
    // 问题2: 如何将posX posY存储到dragEle?

    function DragEle(ele) {
        // {}  创建的实例化对象
        // this => {}
        // this[[Prototype]] = DragEle.prototype;
        this.target = ele;

        this.init();

        // return this;  // {target}
    }
    DragEle.prototype.init = function () {
        this.randColor();
        this.dragStart();  // 构造函数执行时默认绑定按下事件
    }
    DragEle.prototype.dragStart = function () {
        // this -> 调用此方法的实例化对象对象

        // 多个函数嵌套  this指向混乱的问题
        // 1. 提前声明中间变量记录this的指向   _this = this; that = this;
        // 2. 借助箭头函数

        var _this = this; // _this = 调用此方法的实例化对象    提前声明中间变量记录this的指向  

        // 拖拽开始 在目标元素上按下 记录按下的位置 
        this.target.onmousedown = function (e) {
            // this-> 指向绑定事件的元素 shadow

            var e = e || window.event;
            var posX = e.offsetX;
            var posY = e.offsetY;
            console.log(posX, posY);
            _this.posX = posX;
            _this.posY = posY;
            console.log(_this);  // 自己没有找父作用域 =>  调用此方法的实例化对象 

            // 先按下 => 按下时绑定移动和松开事件
            _this.dragMove();
            _this.dragEnd();
        }
    }
    DragEle.prototype.dragMove = function () {
        // 鼠标在文档中移动时,元素跟随鼠标移动
        // this -> dragEle对象 
        document.onmousemove = () => {
            // 箭头函数  => this(依赖于父作用域的this执行 this->dragEle)
            var e = e || window.event;
            var x = e.pageX - this.posX;
            var y = e.pageY - this.posY;
            console.log(x, y);

            this.target.style.left = x + "px";
            this.target.style.top = y + "px";

        }
    }
    DragEle.prototype.dragEnd = function () {
        // 鼠标在文档中任意位置松开时  清除移动
        document.onmouseup = function () {
            document.onmousemove = null;
            document.onmouseup = null;
        }
    }
    DragEle.prototype.randColor = function () {
        var str = "0123456789abcdef";
        var colorStr = "#";
        for (var i = 0; i < 6; i++) {
            var index = Math.floor(Math.random() * str.length);
            var char = str.charAt(index);
            colorStr += char;
        }
        this.target.style.backgroundColor = colorStr;

    }

    var shadowList = document.querySelectorAll(".shadow");

    for (var i = 0; i < shadowList.length; i++) {
        var shadow = shadowList[i];
        var o = new DragEle(shadow);
        console.log(o);
        o.dragStart();
    }








</script>

</html>